msg_tool\scripts\artemis\ast/
types.rs1use std::cmp::{PartialEq, PartialOrd};
2use std::convert::From;
3use std::ops::{Deref, Index, IndexMut};
4
5#[derive(Clone, Debug, PartialEq)]
6pub enum Value {
8 Float(f64),
10 Int(i64),
12 Str(String),
14 KeyVal((Box<Value>, Box<Value>)),
16 Array(Vec<Value>),
18 Null,
20}
21
22impl From<String> for Value {
23 fn from(s: String) -> Self {
24 Value::Str(s)
25 }
26}
27
28impl<'a> From<&'a str> for Value {
29 fn from(s: &'a str) -> Self {
30 Value::Str(s.to_string())
31 }
32}
33
34impl From<i64> for Value {
35 fn from(i: i64) -> Self {
36 Value::Int(i)
37 }
38}
39
40impl From<f64> for Value {
41 fn from(f: f64) -> Self {
42 Value::Float(f)
43 }
44}
45
46pub struct Key<'a>(pub &'a str);
49
50#[derive(Clone, Copy)]
53pub struct NumKey<T: Clone + Copy>(pub T);
54
55impl<'a> Deref for Key<'a> {
56 type Target = str;
57
58 fn deref(&self) -> &Self::Target {
59 &self.0
60 }
61}
62
63const NULL: Value = Value::Null;
64
65impl Value {
66 pub fn as_str(&self) -> Option<&str> {
68 if let Value::Str(s) = self {
69 Some(s)
70 } else {
71 None
72 }
73 }
74
75 pub fn as_string(&self) -> Option<String> {
77 if let Value::Str(s) = self {
78 Some(s.clone())
79 } else {
80 None
81 }
82 }
83
84 pub fn find_array(&self, key: &str) -> &Value {
95 match self {
96 Value::Array(arr) => {
97 for item in arr {
98 if &item[0] == key {
99 return item;
100 }
101 }
102 &NULL
103 }
104 _ => &NULL,
105 }
106 }
107
108 pub fn find_array_mut(&mut self, key: &str) -> &mut Value {
119 match &self {
120 Value::Array(arr) => {
121 for (i, item) in arr.iter().enumerate() {
122 if &item[0] == key {
123 return &mut self[i];
124 }
125 }
126 self.push_member(Value::Array(vec![Value::Str(key.to_string())]));
127 self.last_member_mut()
128 }
129 _ => {
130 *self = Value::Array(vec![Value::Str(key.to_string())]);
131 self.last_member_mut()
132 }
133 }
134 }
135
136 pub fn is_array(&self) -> bool {
138 matches!(self, Value::Array(_))
139 }
140
141 pub fn is_str(&self) -> bool {
143 matches!(self, Value::Str(_))
144 }
145
146 pub fn is_kv(&self) -> bool {
148 matches!(self, Value::KeyVal(_))
149 }
150
151 pub fn is_null(&self) -> bool {
153 matches!(self, Value::Null)
154 }
155
156 pub fn kv_key(&self) -> Option<&Value> {
158 if let Value::KeyVal((k, _)) = self {
159 Some(&k)
160 } else {
161 None
162 }
163 }
164
165 pub fn kv_keys<'a>(&'a self) -> Box<dyn Iterator<Item = &'a Value> + 'a> {
167 match self {
168 Value::KeyVal((k, _)) => Box::new(std::iter::once(&**k)),
169 Value::Array(arr) => Box::new(arr.iter().filter_map(|v| v.kv_key())),
170 _ => Box::new(std::iter::empty()),
171 }
172 }
173
174 pub fn last_member(&self) -> &Value {
176 match self {
177 Value::Array(arr) => arr.last().unwrap_or(&NULL),
178 _ => &NULL,
179 }
180 }
181
182 pub fn last_member_mut(&mut self) -> &mut Value {
187 match self {
188 Value::Array(arr) => {
189 if arr.is_empty() {
190 arr.push(NULL);
191 }
192 arr.last_mut().unwrap()
193 }
194 _ => {
195 *self = Value::Array(vec![NULL]);
196 self.last_member_mut()
197 }
198 }
199 }
200
201 pub fn len(&self) -> usize {
203 match self {
204 Value::Array(arr) => arr.len(),
205 _ => 0,
206 }
207 }
208
209 pub fn arr_len(&self) -> usize {
211 self.members().filter(|s| s.is_array()).count()
212 }
213
214 pub fn insert_member(&mut self, index: usize, value: Value) {
219 match self {
220 Value::Array(arr) => {
221 if index < arr.len() {
222 arr.insert(index, value);
223 } else {
224 arr.push(value);
225 }
226 }
227 _ => {
228 *self = Value::Array(vec![value]);
229 }
230 }
231 }
232
233 pub fn members<'a>(&'a self) -> Iter<'a> {
235 match self {
236 Value::Array(arr) => Iter { iter: arr.iter() },
237 _ => Iter::default(),
238 }
239 }
240
241 pub fn members_mut<'a>(&'a mut self) -> IterMut<'a> {
243 match self {
244 Value::Array(arr) => IterMut {
245 iter: arr.iter_mut(),
246 },
247 _ => IterMut::default(),
248 }
249 }
250
251 pub fn new_array() -> Self {
253 Value::Array(Vec::new())
254 }
255
256 pub fn new_kv<K: Into<Value>, V: Into<Value>>(key: K, value: V) -> Self {
258 Value::KeyVal((Box::new(key.into()), Box::new(value.into())))
259 }
260
261 pub fn push_member(&mut self, value: Value) {
263 match self {
264 Value::Array(arr) => arr.push(value),
265 _ => {
266 *self = Value::Array(vec![value]);
267 }
268 }
269 }
270
271 pub fn set_str<S: AsRef<str> + ?Sized>(&mut self, value: &S) {
273 *self = Value::Str(value.as_ref().to_string());
274 }
275
276 pub fn set_string<S: Into<String>>(&mut self, value: S) {
278 *self = Value::Str(value.into());
279 }
280}
281
282impl Index<usize> for Value {
283 type Output = Value;
284
285 fn index(&self, index: usize) -> &Self::Output {
286 match self {
287 Value::Array(arr) => {
288 if index < arr.len() {
289 &arr[index]
290 } else {
291 &NULL
292 }
293 }
294 _ => &NULL,
295 }
296 }
297}
298
299impl IndexMut<usize> for Value {
300 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
301 match self {
302 Value::Array(arr) => {
303 if index < arr.len() {
304 &mut arr[index]
305 } else {
306 arr.push(NULL);
307 arr.last_mut().unwrap()
308 }
309 }
310 _ => {
311 *self = Value::Array(vec![NULL]);
312 self.index_mut(0)
313 }
314 }
315 }
316}
317
318impl<'a> Index<&'a str> for Value {
319 type Output = Value;
320
321 fn index(&self, key: &'a str) -> &Self::Output {
322 match self {
323 Value::KeyVal((k, v)) if k == key => v,
324 Value::Array(arr) => {
325 for item in arr.iter().rev() {
326 if let Value::KeyVal((k, v)) = item {
327 if k == key {
328 return v;
329 }
330 }
331 }
332 &NULL
333 }
334 _ => &NULL,
335 }
336 }
337}
338
339impl<'a> IndexMut<&'a str> for Value {
340 fn index_mut(&mut self, index: &'a str) -> &mut Self::Output {
341 match &self {
342 Value::KeyVal((k, _)) => {
343 if k == index {
344 if let Value::KeyVal((_, v)) = self {
345 v
346 } else {
347 unreachable!()
348 }
349 } else {
350 *self = Value::KeyVal((Box::new(index.to_string().into()), Box::new(NULL)));
351 if let Value::KeyVal((_, v)) = self {
352 v
353 } else {
354 unreachable!()
355 }
356 }
357 }
358 Value::Array(arr) => {
359 for (i, item) in arr.iter().enumerate().rev() {
360 if let Value::KeyVal((k, _)) = item {
361 if k == index {
362 if let Value::KeyVal((_, v)) = &mut self[i] {
363 return v;
364 } else {
365 unreachable!()
366 }
367 }
368 }
369 }
370 if let Value::Array(arr) = self {
371 arr.push(Value::KeyVal((
372 Box::new(index.to_string().into()),
373 Box::new(NULL),
374 )));
375 if let Value::KeyVal((_, v)) = arr.last_mut().unwrap() {
376 v
377 } else {
378 unreachable!()
379 }
380 } else {
381 unreachable!()
382 }
383 }
384 _ => {
385 *self = Value::Array(vec![Value::KeyVal((
386 Box::new(index.to_string().into()),
387 Box::new(NULL),
388 ))]);
389 self.index_mut(index)
390 }
391 }
392 }
393}
394
395impl<'a> Index<&'a String> for Value {
396 type Output = Value;
397
398 #[inline(always)]
399 fn index(&self, key: &'a String) -> &Self::Output {
400 self.index(key.as_str())
401 }
402}
403
404impl<'a> IndexMut<&'a String> for Value {
405 #[inline(always)]
406 fn index_mut(&mut self, index: &'a String) -> &mut Self::Output {
407 self.index_mut(index.as_str())
408 }
409}
410
411impl Index<String> for Value {
412 type Output = Value;
413
414 #[inline(always)]
415 fn index(&self, key: String) -> &Self::Output {
416 self.index(key.as_str())
417 }
418}
419
420impl IndexMut<String> for Value {
421 #[inline(always)]
422 fn index_mut(&mut self, index: String) -> &mut Self::Output {
423 self.index_mut(index.as_str())
424 }
425}
426
427impl<'a> Index<&'a Value> for Value {
428 type Output = Value;
429
430 fn index(&self, key: &'a Value) -> &Self::Output {
431 match self {
432 Value::KeyVal((k, v)) if k == key => v,
433 Value::Array(arr) => {
434 for item in arr.iter().rev() {
435 if let Value::KeyVal((k, v)) = item {
436 if k == key {
437 return v;
438 }
439 }
440 }
441 &NULL
442 }
443 _ => &NULL,
444 }
445 }
446}
447
448impl<'a> IndexMut<&'a Value> for Value {
449 fn index_mut(&mut self, index: &'a Value) -> &mut Self::Output {
450 match &self {
451 Value::KeyVal((k, _)) => {
452 if k == index {
453 if let Value::KeyVal((_, v)) = self {
454 v
455 } else {
456 unreachable!()
457 }
458 } else {
459 *self = Value::KeyVal((Box::new(index.clone()), Box::new(NULL)));
460 if let Value::KeyVal((_, v)) = self {
461 v
462 } else {
463 unreachable!()
464 }
465 }
466 }
467 Value::Array(arr) => {
468 for (i, item) in arr.iter().enumerate().rev() {
469 if let Value::KeyVal((k, _)) = item {
470 if k == index {
471 if let Value::KeyVal((_, v)) = &mut self[i] {
472 return v;
473 } else {
474 unreachable!()
475 }
476 }
477 }
478 }
479 if let Value::Array(arr) = self {
480 arr.push(Value::KeyVal((Box::new(index.clone()), Box::new(NULL))));
481 if let Value::KeyVal((_, v)) = arr.last_mut().unwrap() {
482 v
483 } else {
484 unreachable!()
485 }
486 } else {
487 unreachable!()
488 }
489 }
490 _ => {
491 *self = Value::Array(vec![Value::KeyVal((
492 Box::new(index.clone()),
493 Box::new(NULL),
494 ))]);
495 self.index_mut(index)
496 }
497 }
498 }
499}
500
501impl<'a> Index<&'a Box<Value>> for Value {
502 type Output = Value;
503
504 #[inline(always)]
505 fn index(&self, key: &'a Box<Value>) -> &Self::Output {
506 self.index(&**key)
507 }
508}
509
510impl Index<NumKey<i64>> for Value {
511 type Output = Value;
512
513 fn index(&self, key: NumKey<i64>) -> &Self::Output {
514 match self {
515 Value::KeyVal((k, v)) if k == key.0 => v,
516 Value::Array(arr) => {
517 for item in arr.iter().rev() {
518 if let Value::KeyVal((k, v)) = item {
519 if k == key.0 {
520 return v;
521 }
522 }
523 }
524 &NULL
525 }
526 _ => &NULL,
527 }
528 }
529}
530
531impl IndexMut<NumKey<i64>> for Value {
532 fn index_mut(&mut self, key: NumKey<i64>) -> &mut Self::Output {
533 match &self {
534 Value::KeyVal((k, _)) => {
535 if k == key.0 {
536 if let Value::KeyVal((_, v)) = self {
537 v
538 } else {
539 unreachable!()
540 }
541 } else {
542 *self = Value::KeyVal((Box::new(key.0.into()), Box::new(NULL)));
543 if let Value::KeyVal((_, v)) = self {
544 v
545 } else {
546 unreachable!()
547 }
548 }
549 }
550 Value::Array(arr) => {
551 for (i, item) in arr.iter().enumerate().rev() {
552 if let Value::KeyVal((k, _)) = item {
553 if k == key.0 {
554 if let Value::KeyVal((_, v)) = &mut self[i] {
555 return v;
556 } else {
557 unreachable!()
558 }
559 }
560 }
561 }
562 if let Value::Array(arr) = self {
563 arr.push(Value::KeyVal((Box::new(key.0.into()), Box::new(NULL))));
564 if let Value::KeyVal((_, v)) = arr.last_mut().unwrap() {
565 v
566 } else {
567 unreachable!()
568 }
569 } else {
570 unreachable!()
571 }
572 }
573 _ => {
574 *self = Value::Array(vec![Value::KeyVal((
575 Box::new(key.0.into()),
576 Box::new(NULL),
577 ))]);
578 self.index_mut(key)
579 }
580 }
581 }
582}
583
584impl Index<NumKey<f64>> for Value {
585 type Output = Value;
586
587 fn index(&self, key: NumKey<f64>) -> &Self::Output {
588 match self {
589 Value::KeyVal((k, v)) if k == key.0 => v,
590 Value::Array(arr) => {
591 for item in arr.iter().rev() {
592 if let Value::KeyVal((k, v)) = item {
593 if k == key.0 {
594 return v;
595 }
596 }
597 }
598 &NULL
599 }
600 _ => &NULL,
601 }
602 }
603}
604
605impl IndexMut<NumKey<f64>> for Value {
606 fn index_mut(&mut self, key: NumKey<f64>) -> &mut Self::Output {
607 match &self {
608 Value::KeyVal((k, _)) => {
609 if k == key.0 {
610 if let Value::KeyVal((_, v)) = self {
611 v
612 } else {
613 unreachable!()
614 }
615 } else {
616 *self = Value::KeyVal((Box::new(key.0.into()), Box::new(NULL)));
617 if let Value::KeyVal((_, v)) = self {
618 v
619 } else {
620 unreachable!()
621 }
622 }
623 }
624 Value::Array(arr) => {
625 for (i, item) in arr.iter().enumerate().rev() {
626 if let Value::KeyVal((k, _)) = item {
627 if k == key.0 {
628 if let Value::KeyVal((_, v)) = &mut self[i] {
629 return v;
630 } else {
631 unreachable!()
632 }
633 }
634 }
635 }
636 if let Value::Array(arr) = self {
637 arr.push(Value::KeyVal((Box::new(key.0.into()), Box::new(NULL))));
638 if let Value::KeyVal((_, v)) = arr.last_mut().unwrap() {
639 v
640 } else {
641 unreachable!()
642 }
643 } else {
644 unreachable!()
645 }
646 }
647 _ => {
648 *self = Value::Array(vec![Value::KeyVal((
649 Box::new(key.0.into()),
650 Box::new(NULL),
651 ))]);
652 self.index_mut(key)
653 }
654 }
655 }
656}
657
658impl<'a, 'b> Index<&'b Key<'a>> for Value {
659 type Output = Value;
660
661 #[inline(always)]
662 fn index(&self, key: &'b Key<'a>) -> &Self::Output {
663 self.find_array(&key.0)
664 }
665}
666
667impl<'a, 'b> IndexMut<&'b Key<'a>> for Value {
668 #[inline(always)]
669 fn index_mut(&mut self, key: &'b Key<'a>) -> &mut Self::Output {
670 self.find_array_mut(&key.0)
671 }
672}
673
674impl<'a> Index<Key<'a>> for Value {
675 type Output = Value;
676
677 #[inline(always)]
678 fn index(&self, key: Key<'a>) -> &Self::Output {
679 self.find_array(&key.0)
680 }
681}
682
683impl<'a> IndexMut<Key<'a>> for Value {
684 #[inline(always)]
685 fn index_mut(&mut self, key: Key<'a>) -> &mut Self::Output {
686 self.find_array_mut(&key.0)
687 }
688}
689
690impl PartialEq<str> for Value {
691 fn eq(&self, other: &str) -> bool {
692 match self {
693 Value::Str(s) => s == other,
694 _ => false,
695 }
696 }
697}
698
699impl PartialEq<String> for Value {
700 fn eq(&self, other: &String) -> bool {
701 self == other.as_str()
702 }
703}
704
705impl PartialEq<i64> for Value {
706 fn eq(&self, other: &i64) -> bool {
707 match self {
708 Value::Int(i) => i == other,
709 _ => false,
710 }
711 }
712}
713
714impl PartialEq<f64> for Value {
715 fn eq(&self, other: &f64) -> bool {
716 match self {
717 Value::Float(f) => f == other,
718 _ => false,
719 }
720 }
721}
722
723impl PartialEq<str> for Box<Value> {
724 #[inline(always)]
725 fn eq(&self, other: &str) -> bool {
726 **self == *other
727 }
728}
729
730impl PartialEq<String> for Box<Value> {
731 #[inline(always)]
732 fn eq(&self, other: &String) -> bool {
733 **self == *other
734 }
735}
736
737impl PartialEq<i64> for Box<Value> {
738 #[inline(always)]
739 fn eq(&self, other: &i64) -> bool {
740 **self == *other
741 }
742}
743
744impl PartialEq<f64> for Box<Value> {
745 #[inline(always)]
746 fn eq(&self, other: &f64) -> bool {
747 **self == *other
748 }
749}
750
751impl PartialEq<Value> for Box<Value> {
752 #[inline(always)]
753 fn eq(&self, other: &Value) -> bool {
754 **self == *other
755 }
756}
757
758impl<'a> PartialEq<i64> for &'a Box<Value> {
759 #[inline(always)]
760 fn eq(&self, other: &i64) -> bool {
761 **self == *other
762 }
763}
764
765impl<'a> PartialEq<f64> for &'a Box<Value> {
766 #[inline(always)]
767 fn eq(&self, other: &f64) -> bool {
768 **self == *other
769 }
770}
771
772impl PartialOrd<i64> for Value {
773 fn partial_cmp(&self, other: &i64) -> Option<std::cmp::Ordering> {
774 match self {
775 Value::Int(i) => i.partial_cmp(other),
776 _ => None,
777 }
778 }
779}
780
781impl PartialOrd<f64> for Value {
782 fn partial_cmp(&self, other: &f64) -> Option<std::cmp::Ordering> {
783 match self {
784 Value::Float(f) => f.partial_cmp(other),
785 _ => None,
786 }
787 }
788}
789
790impl PartialOrd<i64> for Box<Value> {
791 #[inline(always)]
792 fn partial_cmp(&self, other: &i64) -> Option<std::cmp::Ordering> {
793 (**self).partial_cmp(other)
794 }
795}
796
797impl PartialOrd<f64> for Box<Value> {
798 #[inline(always)]
799 fn partial_cmp(&self, other: &f64) -> Option<std::cmp::Ordering> {
800 (**self).partial_cmp(other)
801 }
802}
803
804#[derive(Default)]
805pub struct Iter<'a> {
807 iter: std::slice::Iter<'a, Value>,
808}
809
810impl<'a> Iterator for Iter<'a> {
811 type Item = &'a Value;
812
813 #[inline(always)]
814 fn next(&mut self) -> Option<Self::Item> {
815 self.iter.next()
816 }
817}
818
819impl<'a> ExactSizeIterator for Iter<'a> {
820 #[inline(always)]
821 fn len(&self) -> usize {
822 self.iter.len()
823 }
824}
825
826impl<'a> DoubleEndedIterator for Iter<'a> {
827 #[inline(always)]
828 fn next_back(&mut self) -> Option<Self::Item> {
829 self.iter.next_back()
830 }
831}
832
833#[derive(Default)]
834pub struct IterMut<'a> {
836 iter: std::slice::IterMut<'a, Value>,
837}
838
839impl<'a> Iterator for IterMut<'a> {
840 type Item = &'a mut Value;
841
842 #[inline(always)]
843 fn next(&mut self) -> Option<Self::Item> {
844 self.iter.next()
845 }
846}
847
848impl<'a> ExactSizeIterator for IterMut<'a> {
849 #[inline(always)]
850 fn len(&self) -> usize {
851 self.iter.len()
852 }
853}
854
855impl<'a> DoubleEndedIterator for IterMut<'a> {
856 #[inline(always)]
857 fn next_back(&mut self) -> Option<Self::Item> {
858 self.iter.next_back()
859 }
860}
861
862#[derive(Clone, Debug)]
863pub struct AstFile {
865 pub astver: Option<f64>,
867 pub astname: Option<String>,
869 pub ast: Value,
871}